/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controlador;

import Beans.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author USUARIO
 */
public class ServletServicio extends HttpServlet {

    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String accion = request.getParameter("accion");
        HttpSession session = request.getSession();
        try {
            if(accion.equals("addHorario"))
            {   List<ModIti_Itinerario> listHor;
                if(session.getAttribute("sesHor") != null)
                {   listHor = (List<ModIti_Itinerario>) session.getAttribute("sesHor");  }
                else { listHor = new ArrayList<ModIti_Itinerario>();}
                
                String id = request.getParameter("id");
                if(id != null)
                {   listHor.remove(Integer.parseInt(id));   }
                else 
                {   try
                    {   ModIti_Itinerario it = new ModIti_Itinerario();
                        DateFormat formato = new SimpleDateFormat("HH:mm");
                        Date date;
                        String hora = request.getParameter("hor");
                        date = formato.parse(hora);                    
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(date);
                        it.setHorasalida(cal);
                        listHor.add(it);
                    } catch(ParseException e)
                    {}
                }
                
                session.setAttribute("sesHor", listHor);
                
                out.printf("<table id='example1' class='table table-bordered table-striped'>");
                ModIti_Itinerario iti;
                String h = null;
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                for(int i=0; i<listHor.size(); i++)
                {   iti = listHor.get(i); 
                    h = sdf.format(iti.getHorasalida().getTime());
                    out.printf("<tr>");
                    out.printf("<td>" + (i+1) + "</td>");
                    out.printf("<td>"+h+"</td>");
                    //out.printf("<td>"+iti.getHorasalida().getTime()+"</td>");
                    out.printf("<td><img type'image' src='../../../img/delete.png'  width='25' height='25' title='Descartar' onclick='eliminarHor(" + i + ")' /></td>");            
                    out.printf("</tr>");
                }
                    out.println("</table>");
                
            } else if(accion.equals("addEscala"))
            {   List<ModIti_Agencias> listAge;
                if(session.getAttribute("sesAge") != null)
                {   listAge = (List<ModIti_Agencias>) session.getAttribute("sesAge");  }
                else { listAge = new ArrayList<ModIti_Agencias>();}
                
                String id = request.getParameter("id");
                if(id != null)
                {   listAge.remove(Integer.parseInt(id));   }
                else 
                {   ModIti_Agencias age = new ModIti_Agencias();
                    age.setCiudad(request.getParameter("esc"));//nomEsc
                    listAge.add(age);
                }
                session.setAttribute("sesAge", listAge);
                
                out.printf("<table id='example1' class='table table-bordered table-striped'>");
                ModIti_Agencias age;
                for(int i=0; i<listAge.size(); i++)
                {   age = listAge.get(i);                
                out.printf("<tr>");
                out.printf("<td>" + (i+1) + "</td>");
                out.printf("<td>"+age.getCiudad()+"</td>");
                out.printf("<td><img type'image' src='../../../img/delete.png'  width='25' height='25' title='Descartar' onclick='eliminarEsc(" + i + ")' /></td>");            
                out.printf("</tr>");
                }
                out.println("</table>");
            }
            
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
